//
//  AXFShopViewControllerFillProductCell.m
//  AXFEightGroup
//
//  Created by ya on 12/27/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFShopViewControllerFillProductCell.h"
#import "AXFShoppingButtonView.h"
#import "AXFDataModel.h"

@interface AXFShopViewControllerFillProductCell ()

@property (nonatomic, strong) AXFShoppingButtonView *shoppingButtonView;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation AXFShopViewControllerFillProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style
                reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        [self configutationNotifications];
    }
    return self;
}

- (void)configutationNotifications {

    NotificationCenterAddObserver(self,
                                  Action_AXFShoppingButtonView_AmountChange,
                                  @selector(action_AXFShoppingButtonView_AmountChange:));
}

- (void)action_AXFShoppingButtonView_AmountChange:(NSNotification *)notification {

    if ( !self.model.productTotalPrice ) {
        if (self.removeCellBlock) {
            self.removeCellBlock();
        }
    }

    self.priceLabel.text = [NSString stringWithFormat:@"$%.1f", self.model.productTotalPrice];
}

- (void)dealloc {
    NotificationRemoveObserver(self);
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.shoppingButtonView = nibView(@"AXFShoppingButtonView");
    [self.contentView addSubview:self.shoppingButtonView];

    [self.shoppingButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.centerY.equalTo(self.contentView);
    }];
    [self.shoppingButtonView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];


    self.priceLabel = [UILabel sj_createLabelWithText:@""];
    [self.contentView addSubview:self.priceLabel];
    [self.priceLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shoppingButtonView);
        make.trailing.equalTo(self.shoppingButtonView.mas_leading);
    }];


    self.nameLabel = [UILabel sj_createLabelWithText:@""];
    self.nameLabel.adjustsFontSizeToFitWidth = YES;
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.nameLabel];

    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(8.0);
        make.centerY.equalTo(self.contentView);
        make.trailing.equalTo(self.priceLabel.mas_leading).priority(UILayoutPriorityDefaultLow);
        make.width.equalTo(self.contentView).multipliedBy(0.6);
    }];


    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.height.mas_equalTo(60.0).priority(UILayoutPriorityDefaultLow);
    }];



}


- (void)setModel:(AXFDataModel *)model {
    _model = model;

    self.shoppingButtonView.model = model;
    self.nameLabel.text = model.name;
    self.priceLabel.text = [NSString stringWithFormat:@"$%.1f", self.model.productTotalPrice];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
